<?php
 /**
  * 参加拆封红包活动
  */
global $_GPC, $_W;
$uniacid = $this->_uniacid;
 
$openid = $_W['openid'];
$uid = $_W['member']['uid'];
if (empty($uid)) {
    $fans = mc_fansinfo($openid, 0, $uniacid);
    $uid = $fans['uid'];
}
/*活动配置*/
  $ret = pdo_fetch("select * from ".  tablename("kbwdl_config").
           " where uniacid='$uniacid' and placeid=42 and module='kbwdl_shequzufang'");
  $setting = [];
  if(!empty($ret) && !empty($ret['conf_value'])){
        $setting = iunserializer($ret['conf_value']);     
   } 
   $tid = 0;
   if(!empty($setting) && $setting['isopen']==1 ){
        $tid = intval($setting['tid']);
        $setting = array_filter($setting, "trim");
        $setting['bgimagesrc'] = tomedia("addons/kbwdl_shequzufang/images/hongbao.png");
   }

  if(empty($tid)){ /*没有活动*/
       $this->result(1, '活动未开启', $setting); 
  }  
  $money = intval($setting['money']);/*红包大小*/
  $need = intval($setting['need']);/*'人'*/
  /*活动数据基本*/
  function  get_person( $id,$uniacid){ 
      global $_GPC, $_W;
      $meshare = 0;
      $person = [];
      $per = pdo_fetchall("select * from ". tablename("kbwdl_actlog"). 
            " where acttype=55 and infoid='$id' and uniacid='$uniacid'");    
        foreach($per as $key => $v){
           $tmp = json_decode($v['mark'], true);
           $tmp['avatar'] = tomedia($tmp['avatar']);
           $person[]   = $tmp;
           if($tmp['openid'] == $_W['openid']){
               $meshare = 1;
           }
        }  
     return [$person, $meshare];
  }
  function format_wheel_info($info){
      global $_GPC, $_W;
      if($info['openid'] == $_W['openid']){
          return $info;
      }
      if($info['mehasbang']==1){
        //$info['step'] = 4; 
        //$info['btnstr'] = "我也要参加, 立即开启拆封";
       
          /*我已经参加过*/
        if($info['openid'] !== $_W['openid']) { 
            $info['btnstr'] = "我也要参加, 立即开启拆封";
            $info['nextstep'] = "我已成功帮助了TA一把！";
            $info['step'] = 5; /*我也可以开启*/ 
        }
      }
      return $info;
  }
  /*获取当前活动主题和 助力信息*/
if($_GPC['enews'] == 'initWheel'){
  $pid = intval($_GPC['pid']); 
 
  /*查询活动配置参数信息*/
  $where = " ";
  if($pid){
      $where = " id='$pid' ";
  }else{
      $where = " openid='$openid' ";
  }
  $info = pdo_fetch("select * from ".  tablename("kbwdl_favorite") .
            " where {$where} and ftype='55' and houseid='$tid' and uniacid='$uniacid'"); 
  if($info['openid'] == $openid){
      $info['isme'] = 1;
  }else{
      $info['isme'] = 0;
  }
  $info['tid']= $tid;
  $info['title'] =  $setting['title'];
  $info['thumb'] = tomedia( $setting['paper']);
  $info['setting'] = $setting;
  /*获得用户资料*/
   $info['broker'] = pdo_fetch("select id, nickname,avatar,openid,mobile from " . tablename("kbwdl_broker") .
            " where uniacid='$uniacid' and openid='$openid'");
   $info['broker']['avatar'] = tomedia($info['broker']['avatar']);
   $this->result(0, '初始化参数', $info);  
}
if($_GPC['enews'] == 'friendWheel'){
   /*获取当前好友的助力情况*/
    $pid = intval($_GPC['pid']);
    $info = pdo_fetch("select * from ".  tablename("kbwdl_favorite") .
            " where id='$pid' and ftype='55'  and uniacid='$uniacid'");
    if(empty($info)){
        $this->result(1, '参数错误', $info);  
    }
   /*活动已经开启*/
    $id = intval($info['id']);
       $hasnum = pdo_fetchcolumn("select count(*) from ".tablename("kbwdl_actlog").
               " where  acttype=55 and infoid='$id' and uniacid='$uniacid'");
      $hasneed = $need - $hasnum; 
      $info['actitle'] = "{$info['smalltext']} 邀请您拆封{$money}元红包";
      $info['btnstr'] = "帮TA一把 立马拆封";
      $info['nextstep'] = "需要您的帮助！";
      $info['step'] = 3; /*助力中*/ 
     /*获取参加助力的人列表*/  
   list($person, $meshare) = get_person($info['id'],$uniacid);
   $info['person'] = $person;
   $info['mehasbang'] = $meshare;
   $info['endtime'] = date('Y-m-d H:i:s', $info['last_time']);
   $info['money'] = $money; /*获取奖金*/
   $info['need'] = $need;
   
   $info = format_wheel_info($info);
   
   $this->result(0, 'success', $info); 
}

if($_GPC['enews'] == 'myWheel'){
    /*查询我是否参加过*/

  $tid = intval($_GPC['tid']);
  $info = pdo_fetch("select * from ".  tablename("kbwdl_favorite") .
            " where openid='$openid' and ftype='55' and houseid='$tid' and uniacid='$uniacid'"); 
  if(empty($info)){
      /*还未报名参加*/
      $info['actitle'] = "有{$money}元红包待领，快邀请{$need}位好友来帮拆";
      $info['btnstr'] = "立即开启拆红包";
      $info['nextstep'] = "我也要参加";
      $info['step'] = 1;
  }else{
       $id = intval($info['id']);
      /*活动已经开启*/
       $hasnum = pdo_fetchcolumn("select count(*) from ".tablename("kbwdl_actlog").
               " where  acttype=55 and infoid='$id' and uniacid='$uniacid'");
      $hasneed = $need - $hasnum; 
      $info['actitle'] = "还差{$hasneed}人即可瓜封{$money}元红包，快邀请好友来帮拆";
      $info['btnstr'] = "邀请好友一起拆";
      $info['nextstep'] = "倒计时中";
      $info['step'] = 2; /*助力中*/
     
      /*助力是否完成*/      
       if($hasneed <1){
         $info['actitle'] = "当前共{$hasnum} 位好友助力，获得{$money}元红包！";
         $info['btnstr'] = "奖金领取成功！";
         $info['nextstep'] = "好友们真给力呀";  
         $info['step'] = 9;
       }
       $info['nowtime'] = TIMESTAMP;
       $limttime = intval($info['last_time']);
        /*判断是否到期开始后*/
        if($limttime < TIMESTAMP){
             if($info['step']<9){               
                $info['actitle'] = "还差{$hasneed}人可瓜封{$money}元红包！";
                $info['btnstr'] = "感谢您的参与！";
                $info['nextstep'] = "一步之遥未能领取";                   
                $info['step'] = 8; /*已经报名但是到期*/
                 
             }
        }   
  } 
  
   $info['endtime'] = date('Y-m-d H:i:s', $info['last_time']);
   $info['money'] = $money; /*获取奖金*/
   $info['need'] = $need;

   /*获取参加助力的人列表*/  
   list($person, $meshare) = get_person($info['id'],$uniacid);
   $info['person'] = $person;
   $info['mehasbang'] = $meshare;
   /*获取信息赋值*/
   $info = format_wheel_info($info);
   $this->result(0, 'success', $info); 
}

if ($_GPC['enews'] == 'agentWheel') {
    /*到期活动不给发起*/
    if((strtotime($setting['endtime'])<TIMESTAMP)){
        $this->result(1, '活动已经停止', $setting); 
    }
    /* 我开启拆红包一个主题*/
    $tid = intval($_GPC['tid']);
    $info = pdo_fetch("select * from ".  tablename("kbwdl_favorite") .
            " where openid='$openid' and ftype='55' and houseid='$tid' and uniacid='$uniacid'");
    if(empty($info)){
        $broker = pdo_fetch("select id, nickname,avatar,openid from " . tablename("kbwdl_broker") .
            " where uniacid='$uniacid' and openid='$openid'");
        $thread = [
            'openid'=> $openid,
            'ftype'=> 55, 
            'acttype'=> 'mywheel',
            'addtime'=> TIMESTAMP,
            'last_time'=> TIMESTAMP + 24* 60 * 60, /*24 小时到期*/
            'uid'=> $uid,
            'uniacid'=> $uniacid,
            'houseid'=>  $tid, /*活动主题id*/
            'title' => $_GPC['title'],
            'smalltext'=> $broker['nickname'],
            'hits'=> 1, /*当前助理人数*/
            'url'=> '',
        ];
        pdo_insert('kbwdl_favorite', $thread);
        $id = pdo_insertid();
        /*  这个活动的参加人记录*/
         $save = array(
            'actname'=> 'mywheel',
            'addtime' => date('Y-m-d H:i:s', TIMESTAMP),
            'uniacid'=> $uniacid,
            'acttype'=> 55,
            'money'=> $money,
            'isadd'=>0,
            'note'=> '参加拆红包活动',
            'num' => $broker['id'],/* 参加的brokerid 为待处理*/
            'mark'=>  json_encode($broker),
            'infoid'=> $id,
        );
        pdo_insert('kbwdl_actlog', $save);
        
         $this->result(0, 'success', $thread);    
    }else{
      $this->result( 1, '正在参加中', $info);    
    }
}
/*帮助好友拆一次*/
if($_GPC['enews'] == 'bangzhuWheel'){
     $tid = intval($_GPC['tid']);
     $pid = intval($_GPC['pid']);
     $info = pdo_fetch("select * from ".  tablename("kbwdl_favorite") .
            " where id='$pid' and ftype='55' and houseid='$tid' and uniacid='$uniacid'");
    if(empty($info)){
        $this->result(1, '参数错误', $info);  
    }
    if($info['hits']>= $need){
        $this->result(1, '达到邀请人数上限，成功开封红包', $info);  
    }
      $broker = pdo_fetch("select id, nickname,avatar,openid from " . tablename("kbwdl_broker") .
            " where uniacid='$uniacid' and openid='$openid'");
     /*是否助力过*/
     $hasdo = pdo_fetch("select * from ".  tablename("kbwdl_actlog") .
            " where num = '{$broker['id']}' and  acttype='55' and infoid='$pid' and uniacid='$uniacid'");  
     if(!empty($hasdo)){
        $this->result(1, '已帮助好友拆封过', $broker);   
     }
      if($broker && $broker['openid']){
        $save = array(
            'actname'=> 'mywheel',
            'addtime' => date('Y-m-d H:i:s', TIMESTAMP),
            'uniacid'=> $uniacid,
            'acttype'=> 55,
            'money'=> $money,
            'isadd'=>0,
            'note'=> '参加拆红包活动',
            'num' => $broker['id'],/*1 参加的brokerid*/
            'mark'=>  json_encode($broker),
            'infoid'=> $pid,
        );
        pdo_insert('kbwdl_actlog', $save);
        /*更新助力人数*/
        pdo_update('kbwdl_favorite', ['hits'=> ($info['hits']+1)], array(  'id' => $pid, 'uniacid' => $uniacid));
      }
   $this->result(0, 'success', $broker);       
}
?>
